package com.learnkitchen.learnkitchenrecipes.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

/**
 * Created by Administrator on 2018-01-03.
 */

public class ToastUtil {
    private static String oldMsg;
    private static Toast toast;
    private static long oldTime = 0, currentTime = 0;

    public static void show(Context context, String msg, int time) {
        if (context == null)
            return;
        Toast.makeText(context, msg, time).show();
    }

    public static void show(Context context, int res_id, int time) {
        if (context == null)
            return;
        Toast.makeText(context, context.getResources().getString(res_id), time).show();
    }

    public static void setToastGrivity(int org0) {
        toast.setGravity(org0, 0, 0);
    }

    public static void show(Context context, String msg) {
        if (!TextUtils.isEmpty(msg)) {
            if (toast == null) {
                toast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT);
                toast.show();
                oldTime = System.currentTimeMillis();
            } else {
                currentTime = System.currentTimeMillis();
                if (msg.equals(oldMsg)) {
                    if (currentTime - oldTime > Toast.LENGTH_SHORT) {
                        toast.show();
                    }
                } else {
                    oldMsg = msg;
                    toast.setText(msg);
                    toast.show();
                }
                oldTime = currentTime;
            }
        }
    }

    public static void show(Context context, int id) {
        try {
            show(context, context.getString(id));
        } catch (Exception e) {
            show(context, id + "");
        }
    }

}

